package com.example.administrator.weather;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

import static com.example.administrator.weather.R.id.name;

public class MainActivity extends AppCompatActivity {
    public ImageView mWeatherImage;
    public TextView mWeather, mTemp, mWind, mPm, mName;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWeatherImage = (ImageView) this.findViewById(R.id.weather_img);
        mWeather = (TextView) this.findViewById(R.id.weather);
        mTemp = (TextView) this.findViewById(R.id.temp);
        mWind = (TextView) this.findViewById(R.id.wind);
        mPm = (TextView) this.findViewById(R.id.pm);
        mName = (TextView) this.findViewById(name);


    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void bj(View v) throws IOException {
        InputStream i = getResources().openRawResource(R.raw.weather2);

        StringBuffer sr = new StringBuffer();
        byte[] bytes = new byte[10];
        int len = 0;
        while ((len = i.read(bytes)) != -1) {

            String s = new String(bytes, 0, len);
            sr.append(s);

        }
        String data = sr.toString();

        //Toast.makeText(this,data,Toast.LENGTH_SHORT).show();
        try {
            JSONArray jsonArray = new JSONArray(data);
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject jsonObj=jsonArray.getJSONObject(j);

                String name = jsonObj.getString("name");
                if (name.equals("北京")) {
                    String weather = jsonObj.getString("weather");
                    String pm = jsonObj.getString("pm");
                    String wind = jsonObj.getString("wind");
                    String temp = jsonObj.getString("temp");
                    mWeather.setText(weather);
                    mPm.setText("pm:" + pm);
                    mName.setText(name);
                    mTemp.setText(temp);
                    mWind.setText("风力:" + wind);
                    if (weather.equals("晴")) {
                        mWeatherImage.setImageResource(R.drawable.sun);
                    } else if (weather.equals("晴转多云")) {
                        mWeatherImage.setImageResource(R.drawable.csun);
                    } else if (weather.equals("多云")) {
                        mWeatherImage.setImageResource(R.drawable.cloud);
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void sh(View v) throws IOException {
        InputStream i = getResources().openRawResource(R.raw.weather2);
        StringBuffer sr = new StringBuffer();
        byte[] bytes = new byte[10];
        int len = 0;
        while ((len = i.read(bytes)) != -1) {
            String s = new String(bytes, 0, len);
            sr.append(s);
        }
        String data = sr.toString();
        //Toast.makeText(this,data,Toast.LENGTH_SHORT).show();
        try {
            JSONArray jsonArray = new JSONArray(data);
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject jsonObj=jsonArray.getJSONObject(j);

                String name = jsonObj.getString("name");
                if (name.equals("上海")) {
                    String weather = jsonObj.getString("weather");
                    String pm = jsonObj.getString("pm");
                    String wind = jsonObj.getString("wind");
                    String temp = jsonObj.getString("temp");
                    mWeather.setText(weather);
                    mPm.setText("pm:" + pm);
                    mName.setText(name);
                    mTemp.setText(temp);
                    mWind.setText("风力:" + wind);
                    if (weather.equals("晴")) {
                        mWeatherImage.setImageResource(R.drawable.sun);
                    } else if (weather.equals("晴转多云")) {
                        mWeatherImage.setImageResource(R.drawable.csun);
                    } else if (weather.equals("多云")) {
                        mWeatherImage.setImageResource(R.drawable.cloud);
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void gz(View v) throws IOException {
        InputStream i = getResources().openRawResource(R.raw.weather2);
        StringBuffer sr = new StringBuffer();
        byte[] bytes = new byte[10];
        int len = 0;
        while ((len = i.read(bytes)) != -1) {
            String s = new String(bytes, 0, len);
            sr.append(s);
        }
        String data = sr.toString();
        //Toast.makeText(this,data,Toast.LENGTH_SHORT).show();
        try {
            JSONArray jsonArray = new JSONArray(data);
            for (int j = 0; j < jsonArray.length(); j++) {
                JSONObject jsonObj=jsonArray.getJSONObject(j);

                String name = jsonObj.getString("name");
                if (name.equals("广州")) {
                    String weather = jsonObj.getString("weather");
                    String pm = jsonObj.getString("pm");
                    String wind = jsonObj.getString("wind");
                    String temp = jsonObj.getString("temp");
                    mWeather.setText(weather);
                    mPm.setText("pm:" + pm);
                    mName.setText(name);
                    mTemp.setText(temp);
                    mWind.setText("风力:" + wind);
                    if (weather.equals("晴")) {
                        mWeatherImage.setImageResource(R.drawable.sun);
                    } else if (weather.equals("晴转多云")) {
                        mWeatherImage.setImageResource(R.drawable.csun);
                    } else if (weather.equals("多云")) {
                        mWeatherImage.setImageResource(R.drawable.cloud);
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
